package com.zsck.pos.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import com.chinaums.commoncheck.R;
import com.zsck.pos.view.TitleBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class MVPBaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView, TitleBar.OnClickListener {


    protected P presenter;

    protected abstract P createPresenter();

    private Toast mToast;

    private Unbinder unbinder;

    protected TitleBar mTitleBar;


    protected abstract int getLayoutId();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = createPresenter();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(getLayoutId());

    }

    protected void onOthersInit() {

    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        setContentView(View.inflate(getContext(), getLayoutId(), null));
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        mTitleBar = findViewById(R.id.title_bar);
        if (mTitleBar != null) {
            mTitleBar.setOnClickListener(this);
        }

        unbinder= ButterKnife.bind(this);
        onOthersInit();
    }

    protected Context getContext() {
        return this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initListener();
    }
    /*点击事件和其他*/
    protected void initListener() {

    }

    /*关闭页面*/
    @Override
    public void closePage() {
        this.finish();
    }

    /*弹Toast*/
    @Override
    public void alert(@StringRes int msg) {
        toast(msg);
    }

    @Override
    public void alert(CharSequence msg) {
        toast(msg);
    }

    /*显示进度条*/
    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    protected void toast(@StringRes int msg) {
        toast(getString(msg));
    }

    protected void toast(CharSequence msg) {
        if (mToast == null) {
            mToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    protected void onBackClick() {
        super.onBackPressed();
    }

    /*打开页面*/
    protected void openPage(Class<? extends Activity> clazz) {
        openPage(clazz, false);
    }
    /*打开页面*/
    protected void openPage(Class<? extends Activity> clazz, boolean finishSelf) {
        if (finishSelf) {
            finish();
        }
        startActivity(new Intent(this, clazz));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
        unbinder.unbind();
    }


}
